<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\SwitchGridView;
use App\Admin\Repositories\Product;
use App\Models\Category;
use App\Models\Tag;
use App\Models\Team;
use App\Services\ImageService;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;
class ProductController extends AdminController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {



        return Grid::make(Product::with(['team']), function (Grid $grid){
            $grid->model()->orderBy('sort','asc');



            $grid->selector(function (Grid\Tools\Selector $selector){
                $selector->selectOne('source', '來源', [0=>'自建',1=>'洗牌']);
                $selector->selectOne('team_id', '館所', Team::pluck('name','id'));
                $selector->selectOne('is_secretly', '明/暗配', [
                    '1'=>'明配','2'=>'暗配'
                ]);


            });

            if (request()->get('_view_','list') !== 'list') {
                // 设置自定义视图
                $grid->view('admin.grid.custom');
                $grid->showQuickEditButton();
                $grid->disableEditButton();
            }



            $grid->column('team.name','館所');
            $grid->column('name')->editable();
            $grid->column('img')->image('',request()->get('_view_','list')=='list'?"50":null,request()->get('_view_','list')=='list'?"50":200);
            $grid->column('status')->switch();
            $grid->column('sort')->orderable();
            $grid->column('created_at');
            $grid->tools(function (Grid\Tools $tools) {
                $tools->prepend(new SwitchGridView());

            });


            $grid->quickSearch('name')->placeholder('搜索...');




        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {
            $show->field('id');
            $show->field('team_id');
            $show->field('name');
            $show->field('img');
            $show->field('detail');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::style(
            <<<CSS
            .jstree-proton-responsive .jstree-container-ul{
                display: flex;
            }
            @media (max-width: 768px){
            .jstree-proton-responsive .jstree-container-ul{
                   flex-direction: column;
            }
            
            }
            .jstree-children>.jstree-open>.jstree-anchor .jstree-checkbox{
                display: none!important;
            
            }
            .jstree-proton.jstree-checkbox-no-clicked .jstree-clicked{
            color: #E30087;
            }
            .jstree-proton>.jstree-container-ul>.jstree-node{
            margin-right: 60px;
            }
CSS
        );
        return Form::make(Product::with(['category','tags']), function (Form $form) {
            $form->tab('基本', function (Form $form) {
                $form->select('team_id')->options(Team::pluck('name','id'));
                $form->text('name')->required();
                $form->image('img')->autoUpload()->uniqueName();
                $form->multipleImage('atlas','图集')->autoUpload()->uniqueName()->sortable()->saving(function ($paths) {
                    return json_encode($paths);
                });
                $form->text('height','身高');
                $form->text('weight','體重');
                $form->radio('cup','罩杯')->options([
                    'B'=>'B','C'=>'C','D'=>'D','E'=>'E','G'=>'G+'
                ]);
                $form->radio('scheduling','排班')->options([
                    '早班','晚班','兼差PT'
                ])->required();
                $form->radio('is_secretly','明/暗配')->options([
                    '1'=>'明配','2'=>'暗配'
                ])->default(0);
                $form->checkbox('prohibited_guests','禁客')->options(\App\Models\Product::PROHIBITED_GUESTS)->saving(function ($value) {
                    return $value;
                });
                $form->radio('is_specific_service','預約送禮物')->options([
                    1=>'有明確服務',2=>'無明確服務'
                ])->when(1,function (Form $form){
                    $form->checkbox('gift_specific_service','預約送')->options(\App\Models\Product::SPECIFIC_SERVICE)->when(0,function (Form $form){
                        $form->text('custom_service','自定義贈送')->help('多個請用"|"分开');
                    })->saving(function ($value) {
                        return $value;
                    })->help('最多勾选3个');
                })->when(2,function (Form $form){
                    $form->radio('gift_not_specific_service','預約送')->options(\App\Models\Product::SPECIFIC_NOT_SERVICE)->when(0,function (Form $form){
                        $form->text('custom_service2','自定義贈送');
                    });
                })->default(0);
                /*$form->tree('category','分類')
                    ->nodes(Category::get()->toArray())
                    ->setIdColumn('id')
                    ->setTitleColumn('name')
                    ->setParentColumn('pid')
                    ->customFormat(function ($v) { // 格式化外部注入的值
                        if (!$v) return [];
                        return array_column($v, 'id');
                    });*/

                $form->hidden('sort');
                //$form->hidden('is_index')->default(0);
                /*$form->tags('tags', '標簽')
                    ->pluck('name', 'id')
                    ->options(Tag::all());*/

                //$form->weditor('detail')->simple();

                $form->switch('status');
            })->tab('媒體',function (Form $form){
                $form->file('audio','音频')->autoUpload()->uniqueName()->move('audio');
                $form->hidden('audio_duration','音频')->default(0);
                $form->file('video','视频')->autoUpload()->uniqueName()->move('video');
                $form->image('video_cover','视频封面')->autoUpload()->uniqueName();
            });




            $form->saving(function (Form $form) {
                if($form->audio){
                    $getID3 = new \getID3();
                    $ThisFileInfo = $getID3->analyze(public_path('uploads/'.$form->audio));
                    $fileduration = round($ThisFileInfo['playtime_seconds']);
                    $form->audio_duration = $fileduration;
                }

            });

            $form->saved(function(Form $form, $result){
                if($form->isCreating()){
                    $model = \App\Models\Product::find($result);
                    app(ImageService::class)->resize_product($model);
                    //$model->moveToStart();
                }else{
                    app(ImageService::class)->resize_product($form->model());
                }
            });


        });
    }
}
